<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="../../js/mui.min.js"></script>
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/style.css" />

		<style>
			.fbpyq-div .plus-icon {
				margin-top: -10px;
				display: block;
				width: 70px;
				height: 70px;
				text-align: center;
				line-height: 70px;
				font-size: 40px;
				color: #d8d8d8;
				border: 1px solid #d8d8d8;
			}
			
			#img_div span {
				display: inline-block;
				position: relative;
				top: -23px;
				margin-right: 8px;
			}
			
			#img_div div {
				display: inline-block;
				height: 70px;
				width: 70px;
				margin-right: 8px;
				position: relative;
			}
			
			#img_div div img {
				height: 70px;
				width: 70px;
			}
			
			#img_div div b {
				position: absolute;
				top: -6px;
				right: -6px;
				height: 24px;
				width: 24px;
				background: red;
				border-radius: 50%;
			}
		</style>
	</head>

	<body class="public-fff-bgcolor">
		<!--头部导航-->
		<header class="mui-bar mui-bar-nav wx-nav-bgcolor public-ny-nav" style="background-color: #fe5a3e;">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left">
			</a>
			<h1 class="mui-title">实名认证</h1>
		</header>
		<div class="mui-content public-fff-bgcolor public-padding public-margin">

			<div class="mui-content public-fff-bgcolor public-padding public-margin ">
				<form class="mui-input-group public-before-none">
					<div class="mui-input-row ">

						<input type="text" class="mui-input-clear" id="name" value="" />
					</div>
					<div class="mui-input-row ">

						<input type="text" class="mui-input-clear" id="cardid" value="" />
					</div>

				</form>

			</div>
			<div class="fbpyq-div mui-content public-fff-bgcolor public-padding public-margin">

				<div id="img_div">
					<span class="mui-icon mui-icon-camera plus-icon" id="add_img"></span>
					<img id="imgtmp" style="height: 70px; width: 70px;visibility: hidden;" src="" />

				</div>
				<button class="public-block-btn" id="btn">提交</button>
			</div>

			<script src="../../js/iBase.js"></script>
			<script>
				iBase.init();

				//ibase.id("imgtmp").style.visibility = "hidden";
				var request_url = localStorage.getItem('request_url');
				var imgData = "";
				mui.plusReady(function() {
					var self = plus.webview.currentWebview();
					iBase.id('name').value = self.name;
					iBase.id('cardid').value = self.cardid;
					iBase.post({
						url: request_url + '/api/Face',
						data: {
							action: "chakantouxiang",
							name: iBase.id('name').value,
							cardid: iBase.id('cardid').value,
							headImage: imgData,
							grantOrg: "",
							startDate: "",
							expiryDate: "",
							positiveIDCardImage: "",
							negativeIDCardImage: ""
						},
						succFn: function(data) {

							var json = JSON.parse(eval(data));

							if(json.success) {

								document.getElementById('imgtmp').src = json.data;
								ibase.id("imgtmp").style.visibility = "visible";

							}

						}
					});

				});

				iBase.click(iBase.id('add_img'), function() {

					mui.plusReady(function() {
						var main = plus.android.runtimeMainActivity()
						var Intent = plus.android.importClass("android.content.Intent");
						var intent = new Intent();
						//intent.putExtra("params", "CAMERA_FACING=0");
						intent.setClassName(main, "com.baidu.aip.fl.FaceDetectActivity");

						main.startActivityForResult(intent, 100);
						main.onActivityResult = function(requestCode, resultCode, data) {
							plus.android.importClass(data);
							var bundle = data.getExtras();
							plus.android.importClass(bundle);
							var filepath = bundle.getString("key");

							//iBase.id("imgtmp").src="file://"+filepath;
							var image = new Image();
							image.src = filepath;
							image.onload = function() {
								try {
									imgData = getBase64Image(image);
									document.getElementById('imgtmp').src = "file://" + filepath + "?version=" + new Date().getTime();
									ibase.id("imgtmp").style.visibility = "visible";
								} catch(e) {
									iBase.prompt(e);
								}

							}

						}
					});
				});
				iBase.click(iBase.id('btn'), function() {
					if(imgData.length == 0) {
						iBase.prompt("请采集照片");
						return;
					}
					ibase.id("btn").disabled = true;
					iBase.showWaiting();
					iBase.post({
						url: request_url + '/api/Face',
						data: {
							action: "shimingrenzheng",
							name: iBase.id('name').value,
							cardid: iBase.id('cardid').value,
							headImage: imgData,
							grantOrg: "",
							startDate: "",
							expiryDate: "",
							positiveIDCardImage: "",
							negativeIDCardImage: ""
						},
						succFn: function(data) {
							//console.log(data)
							var json = JSON.parse(eval(data));
							iBase.prompt(json.msg);
							if(json.success) {
								iBase.sendNotice('laowugongrenlist', 'refresh');
								iBase.closeCurrentInterface();
							}
							iBase.closeWaiting();
							ibase.id("btn").disabled = false;

						}
					});

				});

				function getBase64Image(img) {
					var canvas = document.createElement("canvas");
					var width = img.width;
					var height = img.height;
					// calculate the width and height, constraining the proportions
					if(width > height) {
						if(width > 400) {
							height = Math.round(height *= 400 / width);
							width = 400;
						}
					} else {
						if(height > 400) {
							width = Math.round(width *= 400 / height);
							height = 400;
						}
					}
					canvas.width = width; /*设置新的图片的宽度*/
					canvas.height = height; /*设置新的图片的长度*/
					var ctx = canvas.getContext("2d");
					ctx.drawImage(img, 0, 0, width, height); /*绘图*/
					var dataURL = canvas.toDataURL("image/png", 1);
					return dataURL;
				}
			</script>
	</body>

</html>